home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / DoubleVec.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  8KB  |  219 lines

  1. #ifndef DOUBLEVEC_H
  2. #define DOUBLEVEC_H
  3. #pragma once
  4.  
  5. /*
  6.  *    Declarations for Double Precision Vectors
  7.  *
  8.  *    Copyright (C) 1988, 1989.
  9.  *
  10.  *    Dr. Thomas Keffer
  11.  *    Rogue Wave Associates
  12.  *    P.O. Box 85341
  13.  *    Seattle WA 98145-1341
  14.  *
  15.  *    Permission to use, copy, modify, and distribute this
  16.  *    software and its documentation for any purpose and
  17.  *    without fee is hereby granted, provided that the
  18.  *    above copyright notice appear in all copies and that
  19.  *    both that copyright notice and this permission notice
  20.  *    appear in supporting documentation.
  21.  *    
  22.  *    This software is provided "as is" without any
  23.  *    expressed or implied warranty.
  24.  *
  25.  *
  26.  *    @(#)DoubleVec.h    2.1    8/18/89
  27.  */
  28.  
  29. /*    This code is designed to be as compatible as possible with the
  30.  *    NIH Vector classes, while preserving efficiency.  These Vectors
  31.  *    are NOT based on the NIH "Object" class, making them much
  32.  *    smaller.  They also implement reference counting, making them
  33.  *    faster. 
  34.  */
  35.  
  36.  
  37. #include "vdefs.h"
  38. #include <math.h>
  39.  
  40. class istream;
  41. class ostream;
  42. class DoubleVec;
  43.  
  44. class DoubleBlock {
  45.   unsigned short      refs;        // Number of references
  46.   unsigned          npts;        // Number of elements
  47.   double            array[1];    // The data
  48.   friend        DoubleVec;
  49. public:
  50.   DoubleBlock(unsigned n);
  51.   DoubleBlock(unsigned n, double val);
  52.   DoubleBlock(unsigned n, double val, double by);
  53.   ~DoubleBlock();
  54.  
  55.   void            add_reference()    {refs++;}
  56.   unsigned        references()    {return refs;}
  57.   double*        data()        {return array;}
  58. };
  59.  
  60. class DoubleVec {
  61.   DoubleBlock*        block;
  62.   double*        begin;
  63.   unsigned        npts;
  64.   int            step;
  65.  
  66.   static int        numberPerLine; // For printing
  67.   DoubleVec(DoubleVec&, int, unsigned, int); // For slices
  68. protected:
  69.   void            boundsErr(int);
  70.   void            boundsCheck(int);
  71.   void            lengthErr(int);
  72.   void            lengthCheck(int i)    {if(npts!=i) lengthErr(i);}
  73.   void            emptyErr(const char* fname);
  74.   void            sliceErr(unsigned, int, unsigned, int);
  75. public:
  76.   DoubleVec();
  77.   DoubleVec(unsigned n);
  78.   DoubleVec(unsigned n, double val);
  79.   DoubleVec(unsigned n, double val, double by);
  80.   DoubleVec(const DoubleVec& a);
  81.   DoubleVec(const double* dat, unsigned n);  // Copy of dat will be made
  82.   ~DoubleVec();
  83.   
  84.   DoubleVec        slice(int start, unsigned lgt, int strider=1);
  85.   
  86.   double*        data()        {return begin;}
  87.   unsigned        length()    {return npts;}
  88.   int            stride()    {return step;}
  89.  
  90.   DoubleVec&        reference(DoubleVec& v);    // Reference self to v
  91.   DoubleVec        deepCopy();    // copy of self with distinct instance variables 
  92.   DoubleVec        copy()        {return deepCopy();} // Synonym for deepCopy()
  93.   void            deepenShallowCopy();    // Insures only 1 reference to data
  94.   void            resize(unsigned);     // Will pad with zeroes if necessary
  95.  
  96.   void            scanFrom(istream& s); // Read to eof
  97.   void            printOn(ostream& s);  // Pretty print
  98.   void            setFormatting(int);   // Change # items per line
  99.  
  100.   // Indexing:
  101.   double&        operator[](int i);    // With bounds checking
  102.   double&        operator()(int i);    // With optional bounds checking
  103.   
  104.   // Assignment:
  105.   DoubleVec&        operator=(const DoubleVec& v); // Must be same length as v
  106.   DoubleVec&        operator=(double);
  107.   
  108.   // Arithmetic operators:
  109.   DoubleVec&        operator++();
  110.   DoubleVec&        operator--();
  111.   DoubleVec&        operator+=(const DoubleVec&);
  112.   DoubleVec&        operator+=(double);
  113.   DoubleVec&        operator-=(const DoubleVec&);
  114.   DoubleVec&        operator-=(double);
  115.   DoubleVec&        operator*=(const DoubleVec&);
  116.   DoubleVec&        operator*=(double);
  117.   DoubleVec&        operator/=(const DoubleVec&);
  118.   DoubleVec&        operator/=(double);
  119.   
  120.   // Friendly arithmetic operators:
  121.   friend DoubleVec    operator-(const DoubleVec&);
  122.   friend DoubleVec    operator+(const DoubleVec&);
  123.   friend DoubleVec    operator*(const DoubleVec&,const DoubleVec&);
  124.   friend DoubleVec    operator/(const DoubleVec&,const DoubleVec&);
  125.   friend DoubleVec    operator+(const DoubleVec&,const DoubleVec&);
  126.   friend DoubleVec    operator-(const DoubleVec&,const DoubleVec&);
  127.   friend DoubleVec    operator*(const DoubleVec&,double);
  128.   friend DoubleVec    operator*(double,const DoubleVec&);
  129.   friend DoubleVec    operator/(const DoubleVec&,double);
  130.   friend DoubleVec    operator/(double,const DoubleVec&);
  131.   friend DoubleVec    operator+(const DoubleVec&,double);
  132.   friend DoubleVec    operator+(double,const DoubleVec&);
  133.   friend DoubleVec    operator-(const DoubleVec&,double);
  134.   friend DoubleVec    operator-(double,const DoubleVec&);
  135.   
  136.   
  137. #ifndef NO_VECTOR_MATHFUN
  138.   // Math functions:
  139.   DoubleVec    apply(mathFunTy);
  140.   friend    DoubleVec    abs(const DoubleVec&);
  141.   friend    DoubleVec    acos(const DoubleVec&);
  142.   friend    DoubleVec    asin(const DoubleVec&);
  143.   friend    DoubleVec    atan(const DoubleVec&);
  144.   friend    DoubleVec    atan2(const DoubleVec&,const DoubleVec&);
  145.   friend    DoubleVec    ceil(const DoubleVec&);
  146.   friend    DoubleVec    cos(const DoubleVec&);
  147.   friend    DoubleVec    cosh(const DoubleVec&);
  148.   friend    DoubleVec    cumsum(const DoubleVec&);
  149.   friend    DoubleVec    delta(const DoubleVec&);
  150.   friend    double        dot(const DoubleVec&,const DoubleVec&);
  151.   friend    DoubleVec    exp(const DoubleVec&); 
  152.   friend    DoubleVec    floor(const DoubleVec&);
  153.   friend    DoubleVec    log(const DoubleVec&);
  154.   friend    int        max(const DoubleVec&);
  155.   friend    int        min(const DoubleVec&);
  156.   friend    double        mean(const DoubleVec&);
  157.   friend    double        prod(const DoubleVec&);
  158.   friend    DoubleVec    pow(const DoubleVec&,const DoubleVec&);
  159.   friend    DoubleVec    reverse(const DoubleVec&);
  160.   friend    DoubleVec    rint(const DoubleVec&);
  161.   friend    DoubleVec    sin(const DoubleVec&);
  162.   friend    DoubleVec    sinh(const DoubleVec&);
  163.   friend    DoubleVec    sqrt(const DoubleVec&);
  164.   friend    double        sum(const DoubleVec&);
  165.   friend    DoubleVec    tan(const DoubleVec&);
  166.   friend    DoubleVec    tanh(const DoubleVec&);
  167.   friend    double        variance(const DoubleVec&);
  168. #endif
  169.   
  170. };
  171.  
  172. // Other (related) declarations:
  173. DoubleVec    expandEven(const DoubleVec&);
  174. DoubleVec    expandOdd(const DoubleVec&);
  175. ostream&    operator<<(ostream&, const DoubleVec&);
  176. istream&    operator>>(istream&, DoubleVec&);
  177.  
  178. /******************* I N L I N E S **************************/
  179.  
  180. Inline void    DoubleVec::setFormatting(int i){numberPerLine = i;}
  181.  
  182. Inline void    DoubleVec::boundsCheck(int i){
  183.   if(i<0 || i>npts) boundsErr(i);
  184. }
  185. Inline double&    DoubleVec::operator[](int i){
  186.   boundsCheck(i); return begin[i*step];
  187. }
  188. Inline double&    DoubleVec::operator()(int i) {
  189. #if BOUNDS_CHECK    
  190.   boundsCheck(i);
  191. #endif
  192.   return begin[i*step];
  193. }
  194.  
  195. Inline DoubleVec    operator+(const DoubleVec& a)        {return a;}
  196. Inline DoubleVec    operator*(double a, const DoubleVec& b)    {return b*a;}
  197. Inline DoubleVec    operator+(double a, const DoubleVec& b)    {return b+a;}
  198.  
  199. #ifndef NO_VECTOR_MATHFUN
  200. Inline DoubleVec acos(const DoubleVec& V)    { return V.apply(::acos); }
  201. Inline DoubleVec asin(const DoubleVec& V)    { return V.apply(::asin); }
  202. Inline DoubleVec atan(const DoubleVec& V)    { return V.apply(::atan); }
  203. Inline DoubleVec ceil(const DoubleVec& V)    { return V.apply(::ceil); }
  204. Inline DoubleVec cos(const DoubleVec& V)    { return V.apply(::cos); }
  205. Inline DoubleVec cosh(const DoubleVec& V)    { return V.apply(::cosh); }
  206. Inline DoubleVec exp(const DoubleVec& V)    { return V.apply(::exp); }
  207. Inline DoubleVec floor(const DoubleVec& V )      { return V.apply(::floor); }
  208. Inline DoubleVec log(const DoubleVec& V)    { return V.apply(::log); }
  209. Inline DoubleVec rint(const DoubleVec& V)    { return V.apply(::rint); }
  210. Inline DoubleVec sin(const DoubleVec& V)    { return V.apply(::sin); }
  211. Inline DoubleVec sinh(const DoubleVec& V)    { return V.apply(::sinh); }
  212. Inline DoubleVec sqrt(const DoubleVec& V)    { return V.apply(::sqrt); }
  213. Inline DoubleVec tan(const DoubleVec& V)    { return V.apply(::tan); }
  214. Inline DoubleVec tanh(const DoubleVec& V)    { return V.apply(::tanh); }
  215. Inline double mean(const DoubleVec& V)        { return sum(V)/V.length(); }
  216. #endif
  217.  
  218. #endif
  219.